package com.zfl;

public class DiameterOfBinaryTree {

    int maxDiameter;

    public int diameterOfBinaryTree(TreeNode root) {

        getDepth(root);
        return maxDiameter;
    }

    private int getDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftDepth = this.getDepth(root.left);
        int rightDepth = this.getDepth(root.right);

        this.maxDiameter = Math.max(this.maxDiameter, leftDepth + rightDepth);
        //返回当前节点的深度
        return Math.max(leftDepth, rightDepth) + 1;

    }
}
